package fr.chrysalis.ui.components.display
{
	import com.greensock.TweenMax;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import fr.chrysalis.ui.UI;
	import fr.chrysalis.ui.assets.IAsset;
	import fr.chrysalis.ui.components.events.ButtonEvent;
	import fr.chrysalis.ui.components.events.ScrollBarEvent;
	import fr.chrysalis.utils.Debug;

	public class ScrollBar extends Button
	{
		
		/**
		 * Limite en pixel de la barre 
		 */
		public var limit:int;
		
		/**
		 * Largeur du composant 
		 */
		override public function get width():int
		{
			return this.element.width;
		}
		
		/**
		 * Mode d'inclinaisaon de la scrollBar 
		 */
		private var _mode:String;
		
		/**
		 * Reference movieClip extremitée gauche 
		 */
		private var _scrollLeft:MovieClip;
		
		/**
		 * Reference movieClip millieu (bar)
		 */
		private var _scrollMiddle:MovieClip;
		
		/**
		 * Reference movieClip extremitée droite 
		 */
		private var _scrollRight:MovieClip;
		
		/**
		 * Contient la précédente valeur de la position de la souris lors du drag 
		 */
		private var prevPosition:int;
		
		/**
		 * Mode de positionnement
		 */
		public function set mode(value:String):void
		{
			switch(value){
				case ScrollBarMode.HORIZONTAL:
					this.container.rotation = 0;
					break;
				case ScrollBarMode.VERTICAL:
					this.container.rotation = 90;
					break;
			}
			_mode = value;
		}
		
		/**
		 * Mode de positionnement
		 */
		public function get mode():String
		{
			return _mode;
		}
		
		/**
		 * Pourcentage actuel parcouru par la scrollbar ( 0 to 100 )
		 */
		public var pourcent:int;

		
		public function ScrollBar(asset:IAsset)
		{
			super(asset);
			
			this.mode = ScrollBarMode.HORIZONTAL;
			this.pourcent = 0;
			
			this._scrollLeft = this.element.scrollLeft;
			this._scrollMiddle = this.element.scrollMiddle;
			this._scrollRight = this.element.scrollRight;
			
			this.updateSize(100);
			
			this.limit = this.width;
			
		}
		
		/**
		 * Mise à jour de la taille 
		 * @param size taille en px
		 */
		public function updateSize(size:int):void {
			
			if(size < 50){
				size = 50;
			}
			
			var oldSize:int = this._scrollLeft.width + this._scrollMiddle.width + this._scrollRight.width;
			
			this._scrollLeft.x = 0;
			this._scrollMiddle.x = this._scrollLeft.width;
			this._scrollMiddle.width = size - this._scrollLeft.width - this._scrollRight.width;
			this._scrollRight.x = this._scrollMiddle.x + this._scrollMiddle.width;
						
			this.element.area.width = size;
			this.element.bounds.width = size;
			
			this.element.x = this.element.x + (this.pourcent/100) * (oldSize - size);
			
		}
		
		/**
		 * Simule un scroll à un pourcentage donné 
		 * @param pourcent Pourcentage de scroll
		 */
		public function scroll(pourcent:int):void{
			
			if(pourcent < 0){
				pourcent = 0;
			}
			if(pourcent > 100){
				pourcent = 100;
			}
			
			if(this.pourcent != pourcent){
				this.pourcent = pourcent;
				var x:int = this.limit * (this.pourcent / 100) - this.width * (this.pourcent / 100);
				TweenMax.to(this.element, 0.2, {x:x, useFrames: false});
				this.dispatchEvent(new ScrollBarEvent(ScrollBarEvent.MOVE, this.pourcent));
			}
		}
		
		/**
		 * Handler lors d'un MouseEvent.ROLL_OUT  
		 * @param evt
		 */
		override protected function handlerMouseRollOut(evt:MouseEvent):void {
			this.rollOut();
			this.isPressed = false;
		}
		
		/**
		 * Handler lors d'un MouseEvent.MOUSE_DOWN
		 * @param evt
		 */
		override protected function handlerMouseDown(evt:MouseEvent):void {
			this.prevPosition = this.container.mouseX;
			this.element.stage.addEventListener(MouseEvent.MOUSE_UP, handlerMouseUp);
			this.element.stage.addEventListener(MouseEvent.MOUSE_MOVE, handlerMouseMove);
		}
		
		/**
		 * Handler lors d'un MouseEvent.MOUSE_DOWN
		 * @param evt
		 */
		override protected function handlerMouseUp(evt:MouseEvent):void {
			this.scroll(this.pourcent);
			this.element.stage.addEventListener(MouseEvent.MOUSE_UP, handlerMouseUp);
			this.element.stage.removeEventListener(MouseEvent.MOUSE_MOVE, handlerMouseMove);
		}
		
		/**
		 * Handler lors d'un MouseEvent.MOUSE_MOVE
		 * @param evt
		 */
		protected function handlerMouseMove(evt:MouseEvent):void {
				
			var x:int = this.element.x + this.container.mouseX - this.prevPosition;
				
			if(x >= 0 && x <= (this.limit - this.width)){
			
				if(Math.abs(x-this.element.x) > 15){
					TweenMax.to(this.element, 0.2, {x:x, useFrames: false});
				}else{
					this.element.x = x;
				}
				
				this.pourcent = (this.element.x * 100) / (this.limit - this.width);
				
				this.dispatchEvent(new ScrollBarEvent(ScrollBarEvent.MOVE, this.pourcent));
				
				this.prevPosition = this.container.mouseX;
			
			}
			/*
			var global:Point = new Point(this.container.x, this.container.y);
			global = this.container.localToGlobal(global);
			*/
		}
		
		
	}
}